#include<bits/stdc++.h>
#define LL long long
#define LLL __int128
#define uint unsigned
#define ldb long double
#define uLL unsigned long long
using namespace std;
const int N=8e3+5,M=6e4+5;
int n,m,k,L,a[N],D[N][N],f[M],id[M],vp[M];
uint G[N][505];int S[505][65536];
pair<int,int>A[N];
vector<pair<int,int>>vec[M];
inline void seive(int n){
	for(int i=2;i<=n;++i)if(!vp[i]){
		if(i<=10||i%20==3)f[++k]=i,id[i]=k;
		for(int j=i+i;j<=n;j+=i)vp[j]=1;
	}
}
inline void MAIN(){
	cin>>n>>m,L=(n>>4)+1;
	LL ans=0;
	for(int i=1;i<=n;++i){
		int x,y;cin>>x>>y;
		A[i]=make_pair(x,y);
	}
	for(int i=1;i<=n;++i)cin>>a[i],fill(G[i],G[i]+L,0);
	for(int i=0;i<L;++i){
		const int p=i*16;
		for(int j=1;j<65536;++j)
			S[i][j]=S[i][j&j-1]+a[__lg(j&-j)+p];
	}
	for(int i=1;i<=n;++i)
		for(int j=i+1;j<=n;++j)
			D[i][j]=D[j][i]=abs(A[i].first-A[j].first)+abs(A[i].second-A[j].second);
	for(int i=1;i<=n;++i)
		for(int j=i+1;j<=n;++j)
			vec[D[i][j]].emplace_back(i,j);
	for(int k=0;k<=m+m;++k){
		for(const auto&[u,v]:vec[k]){
			if(id[k]){
				for(int i=0;i<L;++i){
					const uint T=G[u][i]^G[v][i];
					ans+=(a[u]+a[v])*__builtin_popcount(T)+S[i][T];
				}
			}
			G[u][v>>4]|=1u<<(v&15),G[v][u>>4]|=1u<<(u&15);
		}
		vec[k].clear();
	}
	cout<<ans<<'\n';
}
signed main(){
	cin.tie(0)->sync_with_stdio(0);
	freopen("chess.in","r",stdin);
	freopen("chess.out","w",stdout);
	seive(60000);
	int t=1;cin>>t;while(t--)MAIN();
	return 0;
}
/*
*/